package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetLotteryNoticeResponse;
import java.util.ArrayList;
import glideHelp.GlideHelper;
import utils.Utils;

public class HomePageAdapter extends RecyclerView.Adapter<HomePageAdapter.ViewHolder> implements View.OnClickListener{
    public ArrayList<GetLotteryNoticeResponse> datas;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private int layoutId;
    private boolean isHorizontal;
    private static boolean changeLine;//是否换行

    public HomePageAdapter(final ArrayList<GetLotteryNoticeResponse> datas, final Context context, int layoutId, boolean isHorizontal) {
        this.datas = datas;
        this.context = context;
        this.layoutId = layoutId;
        this.isHorizontal = isHorizontal;
    }

    public void setDatas(ArrayList<GetLotteryNoticeResponse> datas){
        this.datas = datas;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public ArrayList<GetLotteryNoticeResponse> getDatas(){
        return datas;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.mTextView.setText(datas.get(position).getName());
        viewHolder.frequency.setText(datas.get(position).getDesc());
        GlideHelper.loadImage(context, datas.get(position).getImage_url(), viewHolder.logo);

        if(datas.get(position).getName().equals("排列三") || datas.get(position).getName().equals("福彩3D") || datas.get(position).getName().equals("香港六合彩")) {
            viewHolder.open.setText("");
        } else {
            viewHolder.open.setText(String.format("已开%s期，剩余%s期", datas.get(position).getHad_open_num(), datas.get(position).getRemain_num()));
        }

        viewHolder.time.setText(Utils.getCountTimeByLong((datas.get(position).getTime() - datas.get(position).pasttime)*1000));

        viewHolder.itemView.setTag(position);

        if(isHorizontal){
            if(datas.get(position).isLeft){
                if(viewHolder.left != null){
                    viewHolder.left.setVisibility(View.VISIBLE);
                }
            } else if(datas.get(position).isRight){
                viewHolder.right.setVisibility(View.VISIBLE);
            } else {
                viewHolder.left.setVisibility(View.GONE);
                viewHolder.right.setVisibility(View.GONE);
            }
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            if(dm.heightPixels <= 1280) {
                RelativeLayout.LayoutParams logo_p = (RelativeLayout.LayoutParams) viewHolder.logo.getLayoutParams();
                logo_p.width = Utils.dip2px(context, 60);
                logo_p.height = Utils.dip2px(context, 60);
                viewHolder.logo.setLayoutParams(logo_p);
            }
            int logo_h = viewHolder.logo.getMeasuredHeight();
            int name_h = viewHolder.mTextView.getMeasuredHeight();
            int frequency_h = viewHolder.frequency.getMeasuredHeight();
            int open_h = viewHolder.open.getMeasuredHeight();
            if(logo_h < (name_h + frequency_h + open_h)){
                viewHolder.mTextView.setTextSize(Utils.textHeight2sp(context, logo_h*0.4F));
                viewHolder.frequency.setTextSize(Utils.textHeight2sp(context, logo_h*0.3F));
                viewHolder.open.setTextSize(Utils.textHeight2sp(context, logo_h*0.3F));
            }
        }else{
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            float gap = Utils.dip2px(context, 133)
                    + ((viewHolder.buy_text.getText() == null)? 0 : viewHolder.buy_text.getPaint().measureText(viewHolder.buy_text.getText().toString()))
                    + ((viewHolder.frequency.getText() == null)? 0 : viewHolder.frequency.getPaint().measureText(viewHolder.frequency.getText().toString()))
                    + ((viewHolder.open.getText() == null)? 0 : viewHolder.open.getPaint().measureText(viewHolder.open.getText().toString()));
            if(dm.heightPixels <= 1280 || dm.widthPixels - gap <= 0 || changeLine) {
                if(!changeLine) {
                    changeLine = true;
                }
                viewHolder.lottery_vertical_list.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Utils.dip2px(context, 80)));

                RelativeLayout.LayoutParams logo_p = (RelativeLayout.LayoutParams) viewHolder.logo.getLayoutParams();
                logo_p.width = Utils.dip2px(context, 60);
                logo_p.height = Utils.dip2px(context, 60);
                viewHolder.logo.setLayoutParams(logo_p);

                RelativeLayout.LayoutParams frequency_p = (RelativeLayout.LayoutParams) viewHolder.frequency.getLayoutParams();
                frequency_p.addRule(RelativeLayout.ALIGN_BOTTOM, 0);
                frequency_p.addRule(RelativeLayout.BELOW, viewHolder.mTextView.getId());
                viewHolder.frequency.setLayoutParams(frequency_p);

                RelativeLayout.LayoutParams open_p = (RelativeLayout.LayoutParams) viewHolder.open.getLayoutParams();
                open_p.addRule(RelativeLayout.ALIGN_LEFT, viewHolder.frequency.getId());
                open_p.addRule(RelativeLayout.BELOW, viewHolder.frequency.getId());
                open_p.leftMargin = Utils.dip2px(context, 0);
                open_p.topMargin = Utils.dip2px(context, -8);
                viewHolder.open.setLayoutParams(open_p);
            }
        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public RelativeLayout lottery_vertical_list;
        public TextView mTextView;
        public TextView frequency;
        public ImageView logo;
        TextView open;
        TextView time;
        View left;
        View right;
        TextView buy_text;
        public ViewHolder(View view){
            super(view);
            lottery_vertical_list = view.findViewById(R.id.lottery_vertical_list);
            mTextView = view.findViewById(R.id.name);
            frequency = view.findViewById(R.id.frequency);
            logo = view.findViewById(R.id.logo);
            open = view.findViewById(R.id.open);
            time = view.findViewById(R.id.time);
            buy_text = view.findViewById(R.id.buy_text);
            if(isHorizontal) {
                left = view.findViewById(R.id.left);
                right = view.findViewById(R.id.right);
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
}